﻿using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApplication3.Data.Entities;

namespace WebApplication3.Data
{ 
	public class ApplicationDbContext : IdentityDbContext<AppIdentityUser,AppIdentityRole,int>
	{
		public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
		{
		}
		public DbSet<LoginSeesion> LoginSeesions { get; set; }

		public DbSet<OrderEntity> OrderEntities { get; set; }

		 public DbSet<Product> Products { get; set; }

		protected override void OnModelCreating(ModelBuilder builder)
		{
			builder.Entity<LoginSeesion>(ConfigureLoginSeesion);

			builder.Entity<OrderEntity>(ConfigureOrderEntity);
			builder.Entity<Product>(ConfigureProductEntity);
			base.OnModelCreating(builder);
		}

		private void ConfigureProductEntity(EntityTypeBuilder<Product> builder)
		{
			builder.Property(ci => ci.Id)
				.ForSqlServerUseSequenceHiLo("ProductEntityId")
				.IsRequired();

			builder.Property(ci => ci.ProductName)
				.IsRequired();

			builder.Property(ci => ci.UserId)
				.IsRequired();

			builder.Property(ci => ci.CreatedAt)
				.HasDefaultValue(DateTime.Now);

			builder.Property(ci => ci.UpdatedAt)
				.HasDefaultValue(DateTime.Now);
		}

		private void ConfigureOrderEntity(EntityTypeBuilder<OrderEntity> builder)
		{
			builder.Property(ci => ci.Id)
				.ForSqlServerUseSequenceHiLo("OrderEntityId")
				.IsRequired();
			builder.Property(ci => ci.OpenId)
				.IsRequired();

			builder.Property(ci => ci.ReceiveName) 
				.IsRequired();

			builder.Property(ci => ci.ReceiveMobile)
				.IsRequired();

			builder.Property(ci => ci.ReceiveAddress)
				.IsRequired();

			builder.Property(ci => ci.ProductName)
				.IsRequired();

			builder.Property(ci => ci.WeightGram)
				.IsRequired();


			//builder.Property(ci => ci.CustomerLatitude)
			//.IsRequired();

			//builder.Property(ci => ci.CustomerLongitude)
			//	.IsRequired();

			builder.Property(ci => ci.ReceiveLatitude)
				.IsRequired();

			builder.Property(ci => ci.ReceiveLongitude)
				.IsRequired();


			builder.Property(ci => ci.Status)
				.IsRequired();

			builder.Property(ci => ci.CreatedAt)
				.HasDefaultValue(DateTime.Now);

			builder.Property(ci => ci.UpdatedAt)
				.HasDefaultValue(DateTime.Now);
		}

		private void ConfigureLoginSeesion(EntityTypeBuilder<LoginSeesion> builder)
		{

			builder.Property(ci => ci.Id)
				.ForSqlServerUseSequenceHiLo("LoginSeesionId")
				.IsRequired();
			builder.Property(ci => ci.OpenId)

				.IsRequired();

			builder.Property(ci => ci.SessionKey)
				.IsRequired(true)
				.HasMaxLength(50);

			builder.Property(ci => ci.CreatedAt)
				.HasDefaultValue(DateTime.Now);


		}

	}
	 

}
